package com.zuo.service.serviceImpl;

import com.zuo.entity.Pos;
import com.zuo.mapper.PositionMapper;
import com.zuo.service.PositionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/2 18:24
 */
@Service
@Transactional(readOnly = true)
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    @Override
    public List<Pos> listPosition() {
        return positionMapper.listPosition();
    }

    @Override
    public List<Pos> searchPosition(String pname) {
        return positionMapper.searchPosition(pname);
    }

    @Override
    public Pos getOne(int id) {
        return positionMapper.getOne(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updatePosition(Pos pos) {
        return positionMapper.updatePosition(pos);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int removePositions(String ids) {
        String[] id = ids.split(",");
        int count=0;
        for (String i : id) {
            int pid=0;
            if(StringUtils.isNotBlank(i)){
                pid=Integer.parseInt(i);
            }
            int result = positionMapper.removePosition(pid);
            if(result!=0){
                count++;
            }
        }
        if(count!=id.length){
            throw new RuntimeException("删除失败");
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int addPosition(Pos pos) {
        return positionMapper.addPosition(pos);
    }
}
